package com.baoer.controller;
import com.baoer.R;
import com.baoer.entity.Product;
import com.baoer.entity.ProductState;
import com.baoer.service.ProductService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 获取所有产品列表
     *
     * @return 产品列表
     */
    @GetMapping("/alllist")
    public R list(@RequestBody Map<String, Object> map) {
        return productService.getAllProducts(map);
    }

    /**
     * 获取出境游产品列表
     * @return 出境游产品列表
     */
    @GetMapping("/outbound")
    public R getOutboundProducts(@RequestBody Map<String, Object> map) {
        return productService.getOutboundProducts(map);
    }

    /**
     * 获取周边游产品列表
     * @return 周边游产品列表
     */
    @GetMapping("/around")
    public R getAroundProducts( @RequestBody Map<String, Object> map) {
        return productService.getAroundProducts(map);
    }

    /**
     * 获取其他产品列表
     * @return 其他产品列表
     */
    @GetMapping("/other")
    public R getOtherProducts(@RequestBody Map<String, Object> map) {
        return productService.getOtherProducts(map);
    }

    /**
     * 多条件搜索产品
     */
    @GetMapping("/search")
    public List<Product> searchProducts(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice,
            @RequestParam(required = false) String point,
            @RequestParam(required = false) String destination,
            @RequestParam(required = false) String proid,
            @RequestParam(required = false) Integer storeid,
            @RequestParam(required = false) String duration
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("minPrice", minPrice);
        params.put("maxPrice", maxPrice);
        params.put("point", point);
        params.put("destination", destination);
        params.put("proid", proid);
        params.put("storeid", storeid);
        params.put("duration", duration);

        return productService.searchProducts(params);
    }

    /**
     * 更新产品信息
     */
    @PostMapping("/update")
    public R updateProduct(@RequestBody Product product) {
        int result = productService.updateProduct(product);
        return result > 0 ? R.ok() : R.er();
    }
    
    /**
     * 软删除产品信息
     */
    @PostMapping("/delete/{productId}")
    public R deleteProduct(@PathVariable Integer productId) {
        int result = productService.deleteProduct(productId);
        return result > 0 ?R.ok() : R.er();
    }

    /**
     * 根据产品ID查询产品详情，包含店铺和负责人信息
     */
    @GetMapping("/{id}")
    public R getProductWithStoreAndManager(@PathVariable Integer id) {
        Product product = productService.getProductWithStoreAndManager(id);
        if (product != null) {
            return (R) R.ok(product);
        } else {
            return R.er();
        }
    }

    /**
     * 设置产品状态
     */
    @PostMapping("/setState")
    public R setProductState(@RequestBody ProductState productState) {
        int result = productService.setProductState(productState);
        return result > 0 ? R.ok() : R.er();
    }

    /**
     * 获取最新产品列表
     */
    @GetMapping("/newest")
    public R getNewestProducts(@RequestBody Map<String, Object> map) {
        return productService.getNewestProducts(map);
    }

    /**
     * 获取特价产品列表
     */
    @GetMapping("/special-offer")
    public R getSpecialOfferProducts(@RequestBody Map<String, Object> map) {
        return productService.getSpecialOfferProducts(map);
    }

    /**
     * 获取热卖产品列表
     */
    @GetMapping("/hot-selling")
    public R getHotSellingProducts(@RequestBody Map<String, Object> map) {
        return productService.getHotSellingProducts(map);
    }
}
